#include<stdio.h>
#include<stdbool.h>
#define Size 10
//#include<FiveInRow2.c>

void InintBoard(char(*Board)[Size])//初始化
{
    for(int i=0;i<Size;i++)
    {
        for(int j=0;j<Size;j++)
        {
            Board[i][j]='-';
        }
    }
}


void PrintBoard(char(*Board)[Size])
{
    printf("    ");
    for(int i=0; i<Size;i++)
    {
        printf("%4d",i+1);
    }
    printf("\n");
    
    for(int i=0;i<Size;i++)
    {
        printf("%4d",i+1);
        for(int j=0;j<Size;j++)
        {
            printf("%4c",Board[i][j]);
        }
        printf("\n");
    }
}


bool MakeMove(char (*Board)[Size],int row,int col,char player)
{
    if(row<0||row>=Size||col<0||col>=Size)
    {
        printf("非法位置！请重新输入！\n");
        return false;
    }

    if(Board[row][col]!='-')
    {
        printf("位置非空！请重新输入！\n");
        return false;
    }

    Board[row][col]=player;
    return true;
}


bool CheckIsDrawn(char(*Board)[Size])
{
    for(int i=0;i< Size; i++)
    {
        for(int j=0;j< Size; j++)
        {
            if(Board[i][j]== '-')
            return false;
        }
    }
    return true;
}

//是否是合法位置
bool IsVaLidPlace(int row,int col)
{
    if(row<0||row>=Size||col<0||col>=Size)
    {
        return false;
    }
    return true;
}

bool CheckIsWin(char(*Board)[Size],int row,int col,char player)
{
    //向量数组
    int direction[4][2]=
    {
        {1,0},
        {1,1},
        {0,1},
        {-1,1}
    };
    //遍历4个方向，出现五个棋子则获胜
    for(int i=0;i<4;i++)
    {
        int count = 1;
        //下一颗棋子的位置
        int drow = row + direction[i][0];
        int dclo = col + direction[i][1];
        while(IsVaLidPlace(drow,dclo)&&Board[drow][dclo] == player)
        {
            count++;
            if(count == 5)
            return true;
            drow = drow + direction[i][0];
            dclo = dclo + direction[i][1];
        }
        //反方向找
        drow = row - direction[i][0];
        dclo = col - direction[i][1];
        while(IsVaLidPlace(drow,dclo)&&Board[drow][dclo] == player)
        {
            count++;
            if(count == 5)
            return true;
            drow = drow - direction[i][0];
            dclo = dclo - direction[i][1];
        }
    }
    return false;
}

bool CheckIsCount(char(*Board)[Size],int row,int col,char player,int num)
{
    //向量数组
    int direction[4][2]=
    {
        {1,0},
        {1,1},
        {0,1},
        {-1,1}
    };
    //遍历4个方向，出现五个棋子则获胜
    for(int i=0;i<4;i++)
    {
        int count = 1;
        //下一颗棋子的位置
        int drow = row + direction[i][0];
        int dclo = col + direction[i][1];
        while(IsVaLidPlace(drow,dclo)&&Board[drow][dclo] == player)
        {
            count++;
            if(count == 4)
            return true;
            drow = drow + direction[i][0];
            dclo = dclo + direction[i][1];
        }
        //反方向找
        drow = row - direction[i][0];
        dclo = col - direction[i][1];
        while(IsVaLidPlace(drow,dclo)&&Board[drow][dclo] == player)
        {
            count++;
            if(count == 4)
            return true;
            drow = drow - direction[i][0];
            dclo = dclo - direction[i][1];
        }
    }
    return false;
}

bool AIMakeMove(char(*Board)[Size],int PlayerRow,int Playercol,char player,char AI)
{
    //智能决策  策略
    //优先取胜策略
    for(int i=0;i<Size;i++)
    {
        for(int j=0;j<Size;j++)
        {
            if(Board[i][j]=='-')
            {
                Board[i][j]=AI;
                if(CheckIsWin(Board,i,j,AI)==true)
                {
                    return true;
                }
                Board[i][j]='-';
            }
        }
    }
    //阻止对手策略
    for(int i=0;i<Size;i++)
    {
        for(int j=0;j<Size;j++)
        {
            if(Board[i][j]=='-')
            {
                Board[i][j]=player;
                if(CheckIsWin(Board,i,j,player)==true)
                {
                   Board[i][j]=AI; 
                    return false;
                }
                Board[i][j]='-';
            }
        }
    }
    //阻止对手连城四个
for(int i=0;i<Size;i++)
    {
        for(int j=0;j<Size;j++)
        {
            if(Board[i][j]=='-')
            {
                Board[i][j]=player;
                if(CheckIsWin(Board,i,j,player)==true)
                {
                   Board[i][j]=AI; 
                    return false;
                }
                Board[i][j]='-';
            }
        }
    }

    //阻止对手连城三个
for(int i=0;i<Size;i++)
    {
        for(int j=0;j<Size;j++)
        {
            if(Board[i][j]=='-')
            {
                Board[i][j]=player;
                if(CheckIsCount(Board,i,j,player,3)==true)
                {
                   Board[i][j]=AI; 
                    return false;
                }
                Board[i][j]='-';
            }
        }
    }

       //阻止对手连城两个
for(int i=0;i<Size;i++)
    {
        for(int j=0;j<Size;j++)
        {
            if(Board[i][j]=='-')
            {
                Board[i][j]=player;
                if(
                    CheckIsCount(Board,i,j,player,2)==true)
                {
                   Board[i][j]=AI; 
                    return false;
                }
                Board[i][j]='-';
            }
        }
    } 
    //随机策略
    // int direction[8][2]={0};
    // int count = 0;
    // for(int i=0;i<4;i++)
    // {

    // }



}

int main(int argc,char *argv[])
{
    char Board[Size][Size]={0};
    InintBoard(Board);
    char player='X';
    int row,col;
    while(1)
    {
        PrintBoard(Board);
        
        if(player=='X')
        {
            printf("请|%c|选手开始下棋:",player);
            scanf("%d %d",&row,&col);
            if(MakeMove(Board,row-1,col-1,player) == false)
        {
            continue;
        }
        else
        {
            printf("AI正在下棋...\n");
            if(AIMakeMove(Board,row - 1,col - 1,'x','0')==true)
            {
                printf("AI获胜!\n");
                break;
            }
        }

        }
            if(CheckIsWin(Board,row-1,col-1,player) == true)
        {
            PrintBoard(Board);
            printf("恭喜|%C|选手获胜!\n",player);
            break;
        }

        if(CheckIsDrawn(Board)== true)
        {
            PrintBoard(Board);
            printf("平局!\n");
            break;
        }

        player=(player=='X')?'O':'X';
    }
    return 0;
}